<!DOCTYPE html>
<html>

<head>
  <title>Unit Tests</title>
  <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.16.0.css">
</head>

<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>

  <script src="https://code.jquery.com/qunit/qunit-2.16.0.js"></script>
  <script src="/static/ast.js"></script>
  <script>
    QUnit.test('isPowerExpression should return true for valid power expressions', function (assert) {
      assert.ok(isPowerExpression('a^2'));
      assert.ok(isPowerExpression('b^3'));
      assert.ok(isPowerExpression('x2^4'));
      assert.ok(isPowerExpression('y123^5'));
    });

    QUnit.test('isPowerExpression should return false for invalid power expressions', function (assert) {
      assert.notOk(isPowerExpression('2^3'));
      assert.notOk(isPowerExpression('a^'));
      assert.notOk(isPowerExpression('a^b'));
      assert.notOk(isPowerExpression('a^2*b^3'));
      assert.notOk(isPowerExpression('c^4d'));
    });

    QUnit.test('parseStringToAST should correctly parse the string to AST', function (assert) {
      const ast1 = parseStringToAST('a^2');
      assert.deepEqual(ast1, new Node('power', ['a', 2]), 'AST for "a^2"');

      const ast2 = parseStringToAST('b^3');
      assert.deepEqual(ast2, new Node('power', ['b', 3]), 'AST for "b^3"');

      const ast3 = parseStringToAST('x2^4');
      assert.deepEqual(ast3, new Node('power', ['x2', 4]), 'AST for "x2^4"');
    });

    QUnit.test('parseStringToAST should correctly parse a string with multiple power expressions separated by multiplication', function (assert) {
      const inputString = 'a^2 * b^3 * c^4';
      const ast = parseStringToAST(inputString);
      assert.deepEqual(ast, new Node('multiply', [
        new Node('power', ['a', 2]),
        new Node('power', ['b', 3]),
        new Node('power', ['c', 4])
      ]), 'AST for "a^2 * b^3 * c^4"');
    });

    QUnit.test('parseStringToAST should correctly parse a string with power expressions and regular terms separated by multiplication', function (assert) {
      const inputString = 'a * b^2 * c^3';
      const ast = parseStringToAST(inputString);
      assert.deepEqual(ast, new Node('multiply', [
        'a',
        new Node('power', ['b', 2]),
        new Node('power', ['c', 3])
      ]), 'AST for "a * b^2 * c^3"');
    });

    QUnit.test('parseStringToAST should correctly parse a string with power expressions and regular terms without multiplication', function (assert) {
      const inputString = 'a * b^2* c^3';
      const ast = parseStringToAST(inputString);
      assert.deepEqual(ast, new Node('multiply', [
        'a',
        new Node('power', ['b', 2]),
        new Node('power', ['c', 3])
      ]), 'AST for "a * b^2 c^3"');
    });

    QUnit.test('parseStringToAST should correctly parse a string with a single power expression', function (assert) {
      const inputString = 'x^4';
      const ast = parseStringToAST(inputString);
      assert.deepEqual(ast, new Node('power', ['x', 4]), 'AST for "x^4"');
    });

    QUnit.test('parseStringToAST should correctly parse a string with a single term', function (assert) {
      const inputString = 'y';
      const ast = parseStringToAST(inputString);
      assert.strictEqual(ast, 'y', 'AST for "y"');
    });

    QUnit.test('isPowerExpression should return true for valid power expressions with T and -1', function (assert) {
      assert.ok(isPowerExpression('a^T'));
      assert.ok(isPowerExpression('c^-1'));
      assert.ok(isPowerExpression('d^-'));
    });

    QUnit.test('parseStringToAST should correctly parse the string to AST for T and -1 expressions', function (assert) {
      const ast1 = parseStringToAST('a^T');
      assert.deepEqual(ast1, new Node('transpose', ['a']), 'AST for "a^T"');

      const ast3 = parseStringToAST('c^-1');
      assert.deepEqual(ast3, new Node('inverse', ['c']), 'AST for "c^-1"');

      const ast4 = parseStringToAST('d^-');
      assert.deepEqual(ast4, new Node('inverse', ['d']), 'AST for "d^-"');
    });

    QUnit.start(); // Start the tests

  </script>
</body>

</html>